<?php

/*********************************************
 * Script:	GrandMatrix Functions (modified from "Multi-Dimentional Array Handler (marray) Functions")
 * Author:	Colin Jermain
 * Contact: http://phpknowhow.com/contact.php
 * Date:		January 25th, 2008
 * Version: 1.0
 * License: 
 *
 *		Copyright (C) 2008 Colin Jermain
 *
 *		This program is free software: you can redistribute it and/or modify
 *		it under the terms of the GNU General Public License as published by
 *		the Free Software Foundation, either version 3 of the License, or
 *		(at your option) any later version.
 *
 *		This program is distributed in the hope that it will be useful,
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 *		GNU General Public License for more details.
 *
 *		You should have received a copy of the GNU General Public License
 *		along with this program.	If not, see <http://www.gnu.org/licenses/>.
 *
 * Notes:
 *	(1) Read documentation before using.
 *	(2) Exercise caution because of the use of eval().
 *
*********************************************/

// Process a location string
function GrandMatrix_loc($loc){
	try{
		if(preg_match('/^[A-Za-z0-9_\-]+(,[A-Za-z0-9_\-]+)*$/i', $loc)){
			return preg_replace('/^/', '[\'', preg_replace('/$/', '\']', preg_replace('/,/', '\'][\'', $loc)));
		}else{
			throw new Exception("Invalid location path for multi-dimentional array");
		}
	}catch(Exception $e){
		echo "<b>Fatal Error</b>: ".$e->getMessage()." on line ".$e->getLine()." of ".$e->getFile();
		exit();
	}
}

// Set a value to the location of an array
function GrandMatrix_set(&$array, $loc, $value){
	$value=(is_array($value))?var_export($value, true):"'".addslashes($value)."'";
	eval("\$array".GrandMatrixmar_loc($loc)."={$value};");
}

// Get the value of an array at the location
function GrandMatrix_get(&$array, $loc){
	return eval("return \$array".GrandMatrix_loc($loc).";");
}

// Count the array at the location
function GrandMatrix_count(&$array, $loc){
	return eval("return count(\$array".GrandMatrix_loc($loc).");");
	// Analogous to: return count(GrandMatrix_get($array, $loc));
}

// Return if array is empty at location
function GrandMatrix_is_empty(&$array, $loc){
	return eval("return empty(\$array".GrandMatrix_loc($loc).");");
	// Analogous to: $tmp=GrandMatrix_get($array, $loc); return empty($tmp);
}

// Return if array isset at location
function GrandMatrix_is_set(&$array, $loc){
	return eval("return isset(\$array".GrandMatrix_loc($loc).");");
	// Analogous to: $tmp=GrandMatrix_get($array, $loc); return isset($tmp);
}
?>
